﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_Public;
using QLTV_BLL;

namespace GUI
{
    public partial class uc_HeDaoTao : DevExpress.XtraEditors.XtraUserControl
    {
        #region Khai báo biến

        HeDaoTao_BLL hdt_bll = new HeDaoTao_BLL();
        DataTable dt;
        TextMessageBox box = new TextMessageBox();

        #endregion

        #region Load Form
        public uc_HeDaoTao()
        {
            InitializeComponent();
        }

        private void uc_HeDaoTao_Load(object sender, EventArgs e)
        {
            Load_Data();
        }
        #endregion

        #region Các chức năng
        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmHeDaoTao frm = new frmHeDaoTao();
            if (frm.ShowDialog() == DialogResult.OK)
                Load_Data();
        }

        private void btnSua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grv_Data.RowCount > 0)
            {
                try
                {
                    frmHeDaoTao frm = new frmHeDaoTao();
                    frm.maHeDaoTao = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colMaHe).ToString();
                    if (frm.ShowDialog() == DialogResult.OK)
                        Load_Data();
                }
                catch (Exception)
                {
                    XtraMessageBox.Show(box.focuesFillter,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
            }
            else
                XtraMessageBox.Show(box.NotData,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grv_Data.RowCount > 0)
            {
                HeDaoTao_Public hdt = new HeDaoTao_Public();
                try
                {
                    string maHe = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colMaHe).ToString();
                    hdt.MaHe = maHe;
                    string tenHe = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colTenHe).ToString();
                    dt = hdt_bll.TaoBang(box.sqlCheckExist("DM_Lop", "MaHe", maHe));
                    if (dt.Rows.Count > 0)
                    {
                        XtraMessageBox.Show(box.NotDelete(maHe), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        if (XtraMessageBox.Show(box.Delete_Confirm(maHe, tenHe), box.Caption_Confirm, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                        {
                            try
                            {
                                hdt_bll.Delete(hdt);
                                Load_Data();
                            }
                            catch (Exception)
                            {
                                XtraMessageBox.Show(box.DeleteFail, box.Caption_Fail, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    XtraMessageBox.Show(box.focuesFillter, box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
                XtraMessageBox.Show(box.NotData,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
        }
        #endregion

        #region Hàm viết thêm
        private void Load_Data()
        {
            grd_Data.DataSource = hdt_bll.LayDuLieu();
            //grv_Data.BestFitColumns();
        }

        #endregion
    }
}
